package com.company.project.controller;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.exception.BusinessException;
import com.company.project.common.exception.code.BaseResponseCode;
import com.company.project.common.utils.Constant;
import com.company.project.entity.SysManageEntity;
import com.company.project.entity.SysUser;
import com.company.project.entity.TeamUserEntity;
import com.company.project.service.HttpSessionService;
import com.company.project.service.SysManageService;
import com.company.project.service.TeamUserService;
import com.company.project.service.UserService;

/**
 * 
 * Title: BaseController.java  
 * Description: 获取个人相关信息    
 * @author LS  
 * @date 2021-2-20 16:32:42  
 * @version 1.0
 */
@Controller
public class BaseController {
	
	@Autowired
    private HttpSessionService httpSessionService;
    
    @Autowired
    private UserService userService;
    
    @Resource
	private TeamUserService teamUserService;
    
    @Resource
    private SysManageService sysManageService;
	
    /**
     * 
     * @Des：获取比赛报名状态，1：启动，2：停止
     * @return
     */
    public boolean getMatchStatus() {
    	//表里面默认只是有一条
    	SysManageEntity entity = sysManageService.getById("1");
    	if(entity!=null) {
    		int status = entity.getStatus();
    		if(status==Constant.MATCH_STATUS_START) {
    			return true;
    		}else {
    			return false;
    		}
    	}
    	return false;
    }
    
    /**
     * @Des：获取当前用户
     * @return
     */
	public SysUser getCurrentUser() {
		String userId = httpSessionService.getCurrentUserId();
        SysUser user = userService.getById(userId);
        if (null == user) {
            throw new BusinessException(BaseResponseCode.TOKEN_ERROR);
        }
		return user;
		
	}
	
	/**
     * @Des：获取当前用户所在的团队-成员信息
     * @return
     */
	public TeamUserEntity getCurrentTeamUser() {
		
		TeamUserEntity teamUser = teamUserService
				.getOne(Wrappers.<TeamUserEntity>lambdaQuery().eq(TeamUserEntity::getDcicUserId, getCurrentUser().getDcicUserId()));
		if (teamUser == null) {
			throw new BusinessException(BaseResponseCode.NO_TEAM);
		}	
		return teamUser;
		
	}
	
	/**
     * @Des：判断当前用户是否是队长
     * @return
     */
	public boolean isCaptain() {
		boolean rlt = false;
		TeamUserEntity teamUser = getCurrentTeamUser();
		if (teamUser.getIsCaptain().equals(Constant.IS_TRUE)) {
			rlt = true;
		}  
		return rlt;
	}

}
